package com.engine.jucailinkq.common.service.impl;

import com.engine.jucailinkq.common.cmd.QueryCommonCmd;
import com.engine.jucailinkq.common.service.CommonService;
import com.engine.jucailinkq.common.util.CommonUtil;
import com.engine.jucailinkq.common.util.DbTools;
import com.engine.jucailinkq.common.util.Utils;
import com.engine.core.impl.Service;
import lombok.extern.slf4j.Slf4j;
import weaver.general.Util;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class CommonServiceImpl extends Service implements CommonService {

    @Override
    public Map<String, Object> queryCommon(Map<String, Object> param) {
        return commandExecutor.execute(new QueryCommonCmd(param));
    }


    @Override
    public Map<String, Object> updateCommon(Map<String, Object> param) {
        Map<String, Object> result = new HashMap<>();
        log.debug("updateCommon param:[{}]",param);
        result.put("result", DbTools.update(CommonUtil.makeUpdateSql(Util.null2String(param.get("tableName")), Utils.resolveMap(Util.null2String(param.get("data"))),Utils.resolveMap(Util.null2String(param.get("condition"))))));
        return result;
    }

    @Override
    public Map<String, Object> insertCommon(Map<String, Object> param) {
        Map<String, Object> result = new HashMap<>();
        log.debug("insertCommon param:[{}]",param);
        Map<String,Object> params = Utils.resolveMap(Util.null2String(param.get("params")));
        String tableName = Util.null2String(params.get("tableName"));
        params.remove("tableName");
        result.put("result", CommonUtil.makeInsertSql(tableName,params));
        return result;

    }
}
